import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'

moment.locale('zh-cn')

Vue.filter('NumberFormat', function (value) {
    if (!value) {
        return '0'
    }
    // 将整数部分逢三一断
    return value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD') {
    return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
    return moment(dataStr).format(pattern)
})

// 金额格式化过滤器
Vue.filter('price', function (value, currency = '￥') {
    if (isNaN(value)) return '';
    return `${currency}${Number(value).toFixed(2)}`;
});

// 精度格式化过滤器
Vue.filter('fixed', function (value, digits = 2) {
    if (isNaN(value)) return '';
    return `${Number(value).toFixed(digits)}`;
});

// 精度格式化过滤器
Vue.filter('select', function (value, options = []) {
    if (!value || !Array.isArray(options)) return value;
    const option = options.find(opt => opt.value === value);
    return option ? option.label : value;
});